package com.example.zhongcao.service.impl;

import com.example.zhongcao.entity.*;
import com.example.zhongcao.exception.InvalidParam;
import com.example.zhongcao.exception.User.InvalidOpenid;
import com.example.zhongcao.mapper.*;
import com.example.zhongcao.service.ZhongcaoService;
import com.example.zhongcao.utils.FileUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class ZhongcaoServiceImpl implements ZhongcaoService {
    @Resource
    ZhongcaoMapper zhongcaoMapper;
    @Resource
    UserMapper userMapper;
    @Resource
    PicsMapper picsMapper;
    @Resource
    SignMapper signMapper;
    @Resource
    ZhongcaoSignMapper zhongcaoSignMapper;
    @Resource
    UserGoodMapper userGoodMapper;
    private FileUtil fileUtil=new FileUtil();
    private final static String hotType="HOT";
    private final static String newType="NEW";
    private final static String regionType="REGION";
   // private final String allType="ALL";
    private final String keywordType="KEYWORD";

    @Override
    public void buildZhongcao(String openid, String title, String intro, String address, String author, String price, List<MultipartFile> pics, List<String> signs) throws Exception {
        User user=userMapper.selectByOpenid(openid);
        if(user==null){
            throw new InvalidParam("openid无效");
        }
        if(title.equals("")){
            throw new InvalidParam("标题不能为空哦");
        }else if(intro.equals("")){
            throw new InvalidParam("描述不能为空哦");
        }
        Zhongcao zc=zhongcaoMapper.selectByTitle(title);
        if(zc!=null){
            throw new InvalidParam("您已经拥有该种草喽，可去[编辑种草]页面进行修改!");
        }
        zc=new Zhongcao();
        zc.setOpenid(openid);
        zc.setTitle(title);
        zc.setIntro(intro);
        if(address==null){
            zc.setAddress("");
        }else{
            zc.setAddress(address);
        }
        if(!(author==null)){
            zc.setAuthor(author);
        }
        if(price==null){
            zc.setPrice("");
        }else{
            zc.setPrice(price);
        }
        zhongcaoMapper.insertZhongcao(zc);
        if(pics!=null){
            insertPics(pics,openid,title);
        }
        if(signs!=null){
            insertSigns(signs,title);

        }
    }

    @Override
    public List<YuLanZhongcao> getMyZCs(String openid, String type, String keyword) throws InvalidOpenid, InvalidParam {
        User user=userMapper.selectByOpenid(openid);
        if(user==null){
            throw new InvalidOpenid("openid无效");
        }
        if(type==null || type.equals("")){
            throw new InvalidParam("type不能为空");
        }
        List<Zhongcao> zcs=null;
        if(type.equals(this.hotType)){
            //按照最热门搜索
            zcs=zhongcaoMapper.selectByGoodNum(openid);
        }else if(type.equals(this.newType)){
            //按照时间最新搜索
            zcs=zhongcaoMapper.selectByTime(openid);
        } else if(type.equals(this.keywordType)){
            //按照关键字搜索
            zcs=zhongcaoMapper.selectByKeyword(openid, keyword);
        }else{
            throw new InvalidParam("type参数填写错误");
        }
        List<YuLanZhongcao> ylzcs=new ArrayList<>();
        for (Zhongcao zc:zcs) {
            YuLanZhongcao ylzc=new YuLanZhongcao();
            Integer id=zc.getId();
            String path=picsMapper.selectOnePicByid(id);
            ylzc.setId(zc.getId());
            ylzc.setPath(path);
            ylzc.setTitle(zc.getTitle());
            ylzc.setIntro(zc.getIntro());
            ylzc.setGoodNum(zc.getGoodNum());
            ylzc.setCreateTime(zc.getCreateTime());
            ylzcs.add(ylzc);
        }
        return ylzcs;
    }

    @Override
    public ZhongcaoDetails getZhongcaoDeatils(Integer zhongcaoid) throws InvalidParam {
        Zhongcao zc=zhongcaoMapper.selectByZhongcaoid(zhongcaoid);
        if(zc==null){
            throw new InvalidParam("种草id有误");
        }
        Integer id=zc.getId();
        List<String> pics=picsMapper.selectAllPicsByid(id);
        List<String> signs=zhongcaoSignMapper.selectAllsignsById(id);
        ZhongcaoDetails details=new ZhongcaoDetails();
        details.setId(id);
        details.setTitle(zc.getTitle());
        details.setIntro(zc.getIntro());
        details.setAuthor(zc.getAuthor());
        details.setAddress(zc.getAddress());
        details.setCreateTime(zc.getCreateTime());
        details.setGoodNum(zc.getGoodNum());
        details.setPrice(zc.getPrice());
       // details.setPisc(pics);
        details.setPics(pics);
        details.setSigns(signs);
        return details;
    }

    @Override
    public void editZhongcao(Zhongcao zc, List<MultipartFile> pics, List<String> signs) throws Exception {
        Integer id=zc.getId();
        Zhongcao oldzc=zhongcaoMapper.selectByZhongcaoid(id);
        Field[] fields = oldzc.getClass().getDeclaredFields();
        for (Field f:fields) {
            f.setAccessible(true);
            System.out.println(f);
            if(f.get(zc)!=null){
                f.set(oldzc,f.get(zc));
            }
        }
        zhongcaoMapper.updateZhongcao("zhongcao",oldzc,"id="+id);
        if(pics!=null){
            insertPics(pics,oldzc.getOpenid(),oldzc.getTitle());
        }
        if(signs!=null) {
            insertSigns(signs, oldzc.getTitle());
        }

    }

    @Override
    public List<YuLanZhongcao> getComZCs(String type, String keyword, String sign) throws InvalidParam {
        if(type==null || type.equals("")){
            throw new InvalidParam("类型不能为空");
        }
        List<Zhongcao> zcs=null;
        if(type.equals(this.hotType)){
            //按照热门程度搜索
            zcs=zhongcaoMapper.selectByGoodNumAll();
        }else if(type.equals(this.regionType)){
            //按照标签搜索专区
            if(sign==null || sign.equals("")){
                throw new InvalidParam("标签不能为空");
            }
            zcs=zhongcaoMapper.selecyBySignAll(sign);
        }else if(type.equals(this.keywordType)){
            //按照关键字搜索
            zcs=zhongcaoMapper.selectBykeywordAll(keyword);
        }
        List<YuLanZhongcao> ylzcs=new ArrayList<>();
        for (Zhongcao zc:zcs) {
            YuLanZhongcao ylzc=new YuLanZhongcao();
            Integer id=zc.getId();
            String path=picsMapper.selectOnePicByid(id);
            ylzc.setId(zc.getId());
            ylzc.setPath(path);
            ylzc.setTitle(zc.getTitle());
            ylzc.setIntro(zc.getIntro());
            ylzc.setGoodNum(zc.getGoodNum());
           // ylzc.setCreatTime(zc.getCreateTime());
            ylzc.setCreateTime(zc.getCreateTime());
            ylzcs.add(ylzc);
        }
        return ylzcs;
    }

    @Override
    public void goodZhongcao(Integer zhongcaoid, String openid) throws InvalidOpenid, InvalidParam {
        User user=userMapper.selectByOpenid(openid);
        if(user==null){
            throw new InvalidOpenid("openid无效");
        }
        Zhongcao zc=zhongcaoMapper.selectByZhongcaoid(zhongcaoid);
        if(zc==null){
            throw new InvalidParam("种草id无效");
        }
        UserGood ug=userGoodMapper.selectByOpenidAndZhongcaoid(zhongcaoid,openid);
        if(ug!=null){
            throw new InvalidParam("您已经点过赞喽，继续看看别的吧");
        }
        userGoodMapper.insertUserGood(zhongcaoid,openid);
    }

    private void insertPics(List<MultipartFile> pics,String openid,String title) throws Exception {

        Zhongcao zc=zhongcaoMapper.selectByTitle(title);
        Integer id=zc.getId();
        picsMapper.deleteAllPics(id);
        if(pics!=null){
            for (MultipartFile pic:pics) {
                String dir=openid+"/"+title;
                String path=fileUtil.updatePics(pic,dir);
                picsMapper.insertPic(path,zc.getId());
            }
        }

    }
    private void insertSigns(List<String> signs,String title){
        Zhongcao zc=zhongcaoMapper.selectByTitle(title);
        Integer id=zc.getId();
        zhongcaoSignMapper.deleteAllPics(id);
        if(signs!=null){
            for (String sign:signs) {
                ZhongcaoSign zcs=new ZhongcaoSign();
                zcs.setSignname(sign);
                zcs.setZhongcaoid(zc.getId());
                zhongcaoSignMapper.insertZCS(zcs);
            }
        }

    }

}
